

package com.zf.yichat.controller.packet;

import com.github.pagehelper.PageHelper;
import com.zf.yichat.controller.BaseController;
import com.zf.yichat.dto.response.PacketSaleDto;
import com.zf.yichat.mapper.PacketSaleApiMapper;
import com.zf.yichat.mapper.PacketSaleMapper;
import com.zf.yichat.mapper.PacketVideoRuleMapper;
import com.zf.yichat.model.PacketSale;
import com.zf.yichat.model.PacketVideoRule;
import com.zf.yichat.model.User;
import com.zf.yichat.service.PacketService;
import com.zf.yichat.service.UserService;
import com.zf.yichat.utils.common.DtoChangeUtils;
import com.zf.yichat.utils.response.FsResponse;
import com.zf.yichat.utils.response.FsResponseGen;
import com.zf.yichat.vo.CheckStatus;
import org.hibernate.validator.internal.util.Contracts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.Objects;

/**
 *
 */
@Controller
@RequestMapping("packet/video/rule")
public class PacketVideoRuleController extends BaseController {
    @Autowired
    PacketVideoRuleMapper packetVideoRuleMapper;
    @Autowired
    PacketService packetService;

    @RequestMapping("/index")
    public String index() {
        return "packet/video-rule-index";
    }

    @RequestMapping("save/index")
    public String saveIndex(Long id) {
        getRequest().setAttribute("packetVideoRule", packetVideoRuleMapper.selectByPrimaryKey(id));
        return "packet/video-rule-save";
    }

    @RequestMapping("list")
    @ResponseBody
    public FsResponse list(Integer page, Integer limit, Long userId, String name, Integer status) {
        PageHelper.startPage(page, limit);
        return DtoChangeUtils.getPageList(packetVideoRuleMapper.selectAll(), v -> v);
    }


    @RequestMapping("status/save")
    @ResponseBody
    public FsResponse setStatus(Integer status, Long id) {

        PacketVideoRule rule = new PacketVideoRule();
        rule.setStatus(status);
        rule.setId(id);
        if (rule.getStatus() == 1) {
            //只有一个生效的，将其他生效变未生效
            Example example = new Example(PacketVideoRule.class);
            example.createCriteria().andEqualTo("status", 1);
            PacketVideoRule rule1 = new PacketVideoRule();
            rule1.setStatus(0);
            packetVideoRuleMapper.updateByExampleSelective(rule1, example);

            //更新规则并生效配置
            packetVideoRuleMapper.updateByPrimaryKeySelective(rule);
            packetService.packetVideoGiftBySystem();
        } else {
            packetVideoRuleMapper.updateByPrimaryKeySelective(rule);
        }
        return FsResponseGen.success();
    }

    @RequestMapping("force/save")
    @ResponseBody
    public FsResponse setForce(Integer forceValid, Long id) {

        PacketVideoRule rule = new PacketVideoRule();
        rule.setForceValid(forceValid);
        rule.setId(id);
        packetVideoRuleMapper.updateByPrimaryKeySelective(rule);
        return FsResponseGen.success();
    }

    @RequestMapping("save")
    @ResponseBody
    public FsResponse save(PacketVideoRule rule) {
        if (rule.getStatus() == 1) {
            //只有一个生效的，将其他生效变未生效
            Example example = new Example(PacketVideoRule.class);
            example.createCriteria().andEqualTo("status", 1);
            PacketVideoRule rule1 = new PacketVideoRule();
            rule1.setStatus(0);
            packetVideoRuleMapper.updateByExampleSelective(rule1, example);

            //更新规则并生效配置
            if (Objects.isNull(rule.getId())) {
                rule.setCtime(new Date());
                packetVideoRuleMapper.insert(rule);
            } else {
                packetVideoRuleMapper.updateByPrimaryKeySelective(rule);
            }
            packetService.packetVideoGiftBySystem();
        } else {
            if (Objects.isNull(rule.getId())) {
                rule.setCtime(new Date());
                packetVideoRuleMapper.insert(rule);
            } else {
                packetVideoRuleMapper.updateByPrimaryKeySelective(rule);
            }
        }

        return FsResponseGen.success();
    }
}
